package com.ilove.friend.frame;

import java.io.IOException;

import javax.microedition.lcdui.Image;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.kuix.widget.Screen.ScreenMenu;
import org.kalmeo.util.frame.Frame;

import com.ilove.friend.model.FriendContants;
import com.ilove.friend.model.FriendDataProvider;
import com.ilove.friend.model.FriendList;
import com.ilove.model.SMSender;

public class FriendListFrame implements Frame {
	public static FriendListFrame instance = new FriendListFrame();
	public String sex;// 0 woman, 1 man
	public String provincecode;
	public String provincename;
	String message;
	String destination;
	int currPage = 1;
	int pageSize = FriendContants.pageSize;
	int totalpage = 1;
	boolean isnext = true;
	public String[] friendlist = null;
	private final FriendDataProvider dp = new FriendDataProvider();
	private Screen screen;

	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}

	public void onAdded() {

		try {
			int total = dp.getTotalFriend(provincecode, sex);
			totalpage = (int) Math.ceil((double)total/(double)pageSize);
			screen = Kuix.loadScreen(FriendContants.FRIENDLISTFRAME_SCREEN, dp);
			changePageValue(currPage);
			// Load the screen
			screen = Kuix.loadScreen(FriendContants.FRIENDLISTFRAME_SCREEN, dp);
			screen.setCurrent();
			Text textPage = (Text)screen.getWidget("textPage");
			textPage.setText(currPage + "/" + totalpage);
			int screenWidth = Kuix.getCanvas().getWidth();
			ScreenMenu rightMenu = (ScreenMenu)screen.getSecondMenu();
			Metrics metrics = rightMenu.getPreferredSize(screenWidth);
			int containerWidth = screenWidth - (2*metrics.width)-10;
			Widget container = screen.getWidget("CTM_PAGE");
			container.setAttribute("style", "layout:flowlayout(true, fill);align:center;min-size:"+containerWidth + " 16");
	
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}

	public boolean onMessage(Object identifier, Object[] arg1) {
		if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		} else if ("listnext".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget).getDataProvider();
				if (dataprovider != null) {
					FriendList friendlist = (FriendList)dataprovider;
					try {
							FriendFrame.instance.sex = sex;
							FriendFrame.instance.provincecode = provincecode;
							FriendFrame.instance.provincename = provincename;
							FriendFrame.instance.code = friendlist.code;
							Kuix.getFrameHandler().pushFrame(FriendFrame.instance);
						
					} catch (Exception e) {
					}
				}
			}
		}
		else if ("save".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget)
						.getDataProvider();
				if (dataprovider != null) {
					FriendList friend = (FriendList) dataprovider;
					try {
						message = "HSC7X85 DOWNLOAD_PG " + friend.code;
						destination = "7585";
						Kuix.alert("Bạn tải ảnh phai tra phí (5000 đồng). Bạn có đồng ý ko?)",
										KuixConstants.ALERT_QUESTION
												| KuixConstants.ALERT_YES
												| KuixConstants.ALERT_NO,
										"sendsms", "!exit");
					} catch (Exception e) {
					}
				}
			}
		}
		 else if ("friend".equals(identifier)) {
				Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
				if (focusedWidget instanceof ListItem) {
					DataProvider dataprovider = ((ListItem) focusedWidget)
							.getDataProvider();
					if (dataprovider != null) {
						FriendList friend = (FriendList) dataprovider;
						try {
							message = "HSC7X85 PG " + friend.code;
							destination = "7585";
							Kuix.alert("Bạn kết bạn phai tra phí (5000 đồng). Bạn có đồng ý ko?)",
											KuixConstants.ALERT_QUESTION
													| KuixConstants.ALERT_YES
													| KuixConstants.ALERT_NO,
											"sendsms", "!exit");
						} catch (Exception e) {
						}
					}
				}
			}
		else if ("sendsms".equals(identifier)) {
			System.out.println(message + destination);
			new SMSender(message, destination).send();
			return false;
		}
		else if ("arrLeft".equals(identifier)) {
				if (currPage > 1) currPage--;
				try {
					changePageValue(currPage);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else if ("arrRight".equals(identifier)) {
				if(isnext)
				{
					currPage++;
					try {
						changePageValue(currPage);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		return false;
	}

	public void onRemoved() {
		try {
			currPage = 1;
			ProvinceFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}
	}
	public void changePageValue(int page) throws Exception {
		Text textPage = (Text)screen.getWidget("textPage");
		textPage.setText(currPage + "/" + totalpage);
		Picture pictureLeft = (Picture) screen.getWidget("pic-left");
		Picture pictureRight = (Picture) screen.getWidget("pic-right");
		try {
			if (page == 1) {
				pictureLeft.setImage(Image.createImage("/img/left-dis.png"));
			} else {
				pictureLeft.setImage(Image.createImage("/img/left.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			// get data form 
			friendlist = dp.getFriendList(provincecode, sex, currPage, pageSize);
			if(friendlist !=null)
			{
				dp.setFriendList(friendlist);
				// display
				if (friendlist.length < pageSize) {
					isnext = false;
					pictureRight.setImage(Image.createImage("/img/right-dis.png"));
				} else {
					isnext = true;
					pictureRight.setImage(Image.createImage("/img/right.png"));
				}
			}
			else
			{
				isnext = false;
				pictureRight.setImage(Image.createImage("/img/right-dis.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
